package atguigu.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: DRevelation
 * @Date: 2021/3/16 14:26
 */
public class NIOFileChannel03 {
    public static void main(String[] args) throws Exception {
        try (FileInputStream fileInputStream = new FileInputStream("D:\\gitee\\hello-spring\\src\\main\\java\\atguigu\\nio\\1.txt");
             FileOutputStream fileOutputStream = new FileOutputStream("D:\\gitee\\hello-spring\\src\\main\\java\\atguigu\\nio\\2.txt")
        ) {
            FileChannel fileChannel01 = fileInputStream.getChannel();
            FileChannel fileChannel02 = fileOutputStream.getChannel();

            ByteBuffer byteBuffer = ByteBuffer.allocate(512);
            while (true) {
                // 清除标志位position
                byteBuffer.clear();
                int read = fileChannel01.read(byteBuffer);
                if (read == -1) {
                    break;
                }
                byteBuffer.flip();
                fileChannel02.write(byteBuffer);
            }

        }
    }
}
